package cn.yikaojiuguo.student.support.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * @desc 时间转化的方法
 */
public class VeDate {
	public static long formatpubdate(String pubdate) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		long endTime = System.currentTimeMillis();
		long startTime = 0;
		// Data data=format.
		try {
			Calendar c = Calendar.getInstance();
			c.setTime(format.parse(pubdate));
			startTime = c.getTimeInMillis();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return (long) (endTime - startTime);
	}

	// 输入两次时间差得到返回的时间效果
	public static String formatTimeDuration(long duration) {
		// 秒
		long sec = (duration / 1000) % 60;
		// 分钟
		long min = (duration / 1000 / 60) % 60;
		// 小时
		long hour = (duration / 1000 / 60 / 60) % 24;
		// 天
		long day = duration / 1000 / 60 / 60 / 24;

		if (day > 0 && day < 3) {
			return day + "天前";
		} else if (day >= 3) {
			return new SimpleDateFormat("MM-dd", Locale.CHINA).format(new Date(0 - (duration - System
					.currentTimeMillis())));
		}

		if (hour > 0) {
			return hour + "小时前";
		}

		if (min > 0) {
			return min + "分钟前";
		}
		return sec + "秒前";
	}
}
